<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/groups.php
#	FUNCTION:		group-related classes and functions
#	AUTHOR:			Adrian Liu
#	CREATED:		21/02/2012
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

class Groups{

	private $arrGroup;
	
	/**
	*constructor of Groups Class
	*/
	function __construct(){

		$this -> arrGroup = array();
	}
	
	/**
	*destructor of Groups class
	*/
	function __destruct(){
		
		unset($this -> arrGroup);
	}
	
	/**
	*addGroup() function is to add new group into groups 
	*@param $id is the key of new group
	*@param $arrGroup is the group array
	*/
	public function addGroup($id,$arrGroup){
		
		//$obNewGroup = new Group();
		
		$uid = "";
		
		$gid = "";
		
		if(array_key_exists('u_id',$arrGroup)){
			
			$uid = $arrGroup['u_id'];
		}
		else if(array_key_exists('p_id',$arrGroup)){
			
			$gid = $arrGroup['p_id'];
		}
		
		if(strtolower($uid) == strtolower($id) || strtolower($gid) == strtolower($id)){
			
			if(array_key_exists('g_id',$arrGroup)){
				
				if ($this -> getGroup($arrGroup['g_id']) === false){
					
					$obNewGroup = new Group();
					
					$obNewGroup -> groupLoader($arrGroup);
			
					$this -> arrGroup[$arrGroup['g_id']] = $obNewGroup;
				}
				else{
			
					$this -> arrGroup[$arrGroup['g_id']] -> groupLoader($arrGroup);
				}
			}
		}
	}
	
	/**
	*addNewsletterGroup() function is to add new newsletter group to groups
	*@param $id is the key of new group
	*@param $arrGroup is the group array
	*/
	public function addNewsletterGroup($id,$arrGroup){
		
		$uid = "";
		
		$gid = "";
		
		if(array_key_exists('u_id',$arrGroup)){
			
			$uid = $arrGroup['u_id'];
		}
		else if(array_key_exists('p_id',$arrGroup)){
			
			$gid = $arrGroup['p_id'];
		}
		
		if(strtolower($uid) == strtolower($id) || strtolower($gid) == strtolower($id)){
			
			if(array_key_exists('ng_id',$arrGroup)){
				
				if ($this -> getGroup($arrGroup['ng_id']) === false){
					
					$obNewGroup = new NewLetterGroup();
					
					$obNewGroup -> groupLoader($arrGroup);
			
					$this -> arrGroup[$arrGroup['ng_id']] = $obNewGroup;
				}
				else{
			
					$this -> arrGroup[$arrGroup['ng_id']] -> groupLoader($arrGroup);
				}
			}
		}
	}
	
//	/**
//	*groupsLoader() is the function to load groups data throught given groups array
//	*
//	* @param $id is the id of current groups
//	* @param $arrGroups is the data array 
//	*/
//	public function groupsLoader($id = "",$arrGroups = array()){
//		
//		foreach($arrGroups as $Group){
//			
//			$this -> addGroup($id,$Group);
//			
//		}
//	}
	
	/**
	*getGroup() function is to get group object by given group id
	*@param $groupID is the id of group
	*@return group object/false
	*/
	public function getGroup($groupID){
		
		if (is_string($groupID) || is_integer($groupID)) {

			# If set
			if (array_key_exists($groupID, $this -> arrGroup)) {

				# Return page object
				return $this -> arrGroup[$groupID];

			} else {

				# Return failure
				return false;
			}
		} else {

			return false;
		}
	}
	
	public function getGroupArray(){
		
		return $this -> arrGroup;
	}
	
	/**
	*getGroupList() get list of group and return them as array
	*
	*/
	public function getGroupList(){
		
		$arrGroupList = array();
		
		foreach($this -> arrGroup as $obGroup){
			
			$arrGroupList[]= $obGroup -> getID();
		}
		
		return $arrGroupList;
	}
	
	/**
	*getEditableGroups() get data of groups which are editable for this user
	*@return $result groups data array
	*/
	public function getEditableGroups($userID){
		
		global $PROJECT_DB_TABLES;
		
		global $USER;
		
		$sql = "SELECT distinct g.g_id,g.g_name as g_name ";
			
		$sql .= "FROM ".$PROJECT_DB_TABLES["users"]." u ";
			
		$sql .= "INNER JOIN ".$PROJECT_DB_TABLES["usergroup"]." ug on u.u_id = ug.ug_u_id ";
			
		$sql .= "INNER JOIN ".$PROJECT_DB_TABLES["groups"]." g on ug.ug_g_id = g.g_id ";
			
		$sql .= "INNER JOIN ".$PROJECT_DB_TABLES["editgroup"]." eg on g.g_id = eg.eg_eg_id ";
			
		$sql .= "WHERE u_id = '".$userID."' AND ";
			
		$arrUserGroups = $USER -> getGroup()->getGroupArray();
			
		$index = 0;
			
		foreach($arrUserGroups as $objUserGroup){
			
			if($index == 0){
			
				$sql .= "eg.eg_g_id = '".$objUserGroup -> getID()."' ";
			}
			else{
				
				$sql .= " OR eg.eg_g_id = '".$objUserGroup -> getID()."' ";
			}
			
			$index ++;
		}
		
		$result = getRecord($sql);
		
		return $result;
	}
	
	public function hasGroupByName($name){
		
		foreach($this -> arrGroup as $key => $value){
			
			if($value -> getName() == $name){
				
				return true;
			}
		}
		
		return false;
	}
}

class Group{
	
	private $g_id;
	private $g_name;
	private $g_created;
	private $g_modified;
	private $g_homepage;
	private $g_can_lock;
	private $g_can_approve;
	private $g_editgroup;
	private $g_editgroupname;
	
	function __construct(){
		
		$this -> g_id = "";
		$this -> g_created = "";
		$this -> g_modified = "";
		$this -> g_homepage = "";
		$this -> g_name = "";
		$this -> g_can_lock = "";
		$this -> g_can_approve = "";
		$this -> g_editgroup = array();
		$this -> g_editgroupname = array();
	}
	
	function groupLoader($arrGroup){
		
		$flag = false;//flag to show whether current group exist
		
		if(array_key_exists('g_id',$arrGroup)){
			
			if($this -> g_id == $arrGroup['g_id']){
				
				$flag = true;
			}
			else{
				
				$this -> g_id = $arrGroup['g_id'];
			}
		}
		
		if(array_key_exists('g_name',$arrGroup) && !$flag)
			$this -> g_name= $arrGroup['g_name']; 
		
		if(array_key_exists('g_created',$arrGroup) && !$flag)
			$this -> g_created = $arrGroup['g_created']; 
		
		if(array_key_exists('g_modified',$arrGroup) && !$flag)
			$this -> g_modified = $arrGroup['g_modified'];
		
		if(array_key_exists('g_homepage',$arrGroup) && !$flag)
			$this -> g_homepage = $arrGroup['g_homepage'];
		
		if(array_key_exists('g_can_lock',$arrGroup) && !$flag)
			$this -> g_can_lock = $arrGroup['g_can_lock'];
		
		if(array_key_exists('g_can_approve',$arrGroup) && !$flag)
			$this -> g_can_approve = $arrGroup['g_can_approve'];

		#find group id of current user
		if(array_key_exists('eg_eg_id',$arrGroup)){

			if(count($this -> g_editgroup) == 0){
				
				$this -> g_editgroup[$arrGroup['eg_eg_id']] = $arrGroup['eg_eg_id'];
			}
			else{
				
				if(!array_key_exists('eg_eg_id',$this -> g_editgroup)){
					
					$this -> g_editgroup[$arrGroup['eg_eg_id']] = $arrGroup['eg_eg_id'];
				}
			}
		}
		
		#find group id of current user
		if(array_key_exists('eg_eg_name',$arrGroup)){

			if(count($this -> g_editgroup) == 0){
				
				$this -> g_editgroupname[$arrGroup['eg_eg_id']] = $arrGroup['eg_eg_name'];
			}
			else{
				
				if(!array_key_exists('eg_eg_id',$this -> g_editgroup)){
					
					$this -> g_editgroupname[$arrGroup['eg_eg_id']] = $arrGroup['eg_eg_name'];
				}
			}
		}
	}
	
	function __destruct(){
		
		unset($this -> g_id);
		unset($this -> g_created);
		unset($this -> g_modified);
		unset($this -> g_homepage);
		unset($this -> g_can_lock);
		unset($this -> g_can_approve);
		unset($this -> g_editgroup);
		unset($this -> g_editgroupname);
		
	}
	
	public function getID(){
		
		return $this -> g_id;
	}
	
	public function getName(){
		
		return $this -> g_name;
	}
	
	public function getCanApprove(){
		
		return $this -> g_can_approve;
	}
	
	public function getCanLock(){
		
		return $this -> g_can_lock;
	}
	
	public function getEditGroup(){
		
		return $this -> g_editgroup;
	}
	
	public function getEditGroupName(){
		
		return $this -> g_editgroupname;
	}
}

class NewLetterGroup{
	
	private $ng_id;
	private $ng_name;
	private $ng_created;
	private $ng_modified;
	private $ng_groups;
	
	function __construct(){
		
		$this -> ng_id = "";
		$this -> ng_name = "";
		$this -> ng_created = "";
		$this -> ng_modified = "";
		$this -> ng_groups = "";
	}
	
	function __destruct(){
		
		unset($this -> ng_id);
		unset($this -> ng_name);
		unset($this -> ng_created);
		unset($this -> ng_modified);
		unset($this -> ng_groups);
	}
	
	/**
	*groupLoader() load newsletter group into class
	*@param $arrGroup group array
	*/
	function groupLoader($arrGroup){
		
		$flag = false;//flag to show whether current group exist
		
		if(array_key_exists('ng_id',$arrGroup)){
			
			if($this -> ng_id == $arrGroup['ng_id']){
				
				$flag = true;
			}
			else{
				
				$this -> ng_id = $arrGroup['ng_id'];
			}
		}
		
		if(array_key_exists('ng_name',$arrGroup) && !$flag)
			$this -> ng_name = $arrGroup['ng_name'];
		
		if(array_key_exists('ng_created',$arrGroup) && !$flag)
			$this -> ng_created = $arrGroup['ng_created']; 
		
		if(array_key_exists('ng_modified',$arrGroup) && !$flag)
			$this -> ng_modified = $arrGroup['ng_modified'];

		#find group id of current user
		if(array_key_exists('ng_groups',$arrGroup)){

			if(count($this -> ng_groups) == 0){
				
				$this -> ng_groups[$arrGroup['ng_groups']] = $arrGroup['ng_groups'];
			}
			else{
				
				if(!array_key_exists('ng_groups',$this -> g_editgroup)){
					
					$this -> ng_groups[$arrGroup['ng_groups']] = $arrGroup['ng_groups'];
				}
			}
		}
	}
	
	/**
	*getID() get id of current newsletter group
	*@return id of newsletter group
	*/
	public function getID(){
		
		return $this -> ng_id;
	}
}

?>